package edu.mju.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.mju.eduservice.client.VodClient;
import edu.mju.eduservice.entity.StudyVideo;
import edu.mju.eduservice.mapper.StudyVideoMapper;
import edu.mju.eduservice.service.StudyVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author xiaoxin
 * @since 2020-08-31
 */
@Service
public class StudyVideoServiceImpl extends ServiceImpl<StudyVideoMapper, StudyVideo> implements StudyVideoService {

    @Autowired
    private VodClient vodClient ;

    /**
     * 删除小节
     * @param courseId
     */
    @Override
    public void deleteVideo(String courseId) {
        //查询课程id所有视频id
        QueryWrapper<StudyVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        //查询指定列
        wrapperVideo.select("video_source_id");
        List<StudyVideo> studyVideoList = baseMapper.selectList(wrapperVideo);
        //List<StudyVideo>转型List<String>
        List<String> videoIdList = new ArrayList<>();
        for( int i = 0 ; i < studyVideoList.size() ; i++){
            StudyVideo studyVideo = studyVideoList.get(i);
            String videoSourceId = studyVideo.getVideoSourceId();
            //videoId可能为null
            if(!StringUtils.isEmpty(videoSourceId)){
                videoIdList.add(videoSourceId);
            }

        }

        //删除课程id下的所有视频
        //有可能没有小节
        if(videoIdList.size()>0){
            vodClient.deleteAllVideo(videoIdList);
        }


        QueryWrapper<StudyVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }

    @Override
    public void deleteVideoById(String videoId) {
        vodClient.removeVideo(videoId);

    }
}
